﻿Imports System
Imports System.Data.SqlClient
Imports System.Data

Public Class SeasonMembership
    Inherits System.Web.UI.Page

    'Open the connection to the database
    'Dim connectionString As String = "Data Source=(localdb)\Projects;Initial Catalog=CIS441WinterParkWeb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False"
    Dim connectionString As String = "Data Source=SHANK-PC\SQLEXPRESS;Initial Catalog=WinterPark;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"
    Dim connection As New SqlConnection(connectionString)
    Dim com As SqlCommand
    Dim str As String

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            loadCombos()
            PriceTextBox.Text = ""
        End If
    End Sub
    Protected Sub loadCart()
        CartListBox.Items.Clear()

        connection.Open()
        str = "select ItemType,Price from CustomerCart where CustomerID=" & Session("CustomerID")
        com = New SqlCommand(str, connection)
        Dim reader As SqlDataReader = com.ExecuteReader
        While reader.Read
            CartListBox.Items.Add(reader("ItemType"))
        End While
        reader.Close()
    End Sub
    Protected Sub loadCombos()
        'clearing controls
        rdo_descriptions.Items.Clear()
        MembershipTypeDropDownList.Items.Clear()
        CustomerNameDropdownList.Items.Clear()

        'loading membershipcode types
        connection.Open()
        str = "select '#Choose A Membership Type#' as MembershipType,-1 as Membershipid,'' as memDetails union select MembershipType,Membershipid, MembershipType + '-' + convert(varchar,unitPrice) +  ' - ' + coalesce(MembershipDetails,'') as memDetails from MembershipCodesTable order by MembershipType "
        com = New SqlCommand(str, connection)
        Dim reader As SqlDataReader = com.ExecuteReader
        While reader.Read
            MembershipTypeDropDownList.Items.Add(New ListItem(reader("MembershipType"), reader("Membershipid")))
            If reader("Membershipid") <> -1 Then
                rdo_descriptions.Items.Add(New ListItem(reader("memDetails"), reader("Membershipid")))
            End If
        End While
        reader.Close()

        If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If
        'load radio descriptions


        'load customers
        str = " select '#Choose A Customer#' as CustomerName,-1 as CustomerID   union  select Surname + ', ' + FirstName as CustmerName,CustomerId  from CustomerTable order by CustomerName "
        com = New SqlCommand(str, connection)
        reader = com.ExecuteReader
        While reader.Read
            Dim lstItem As New ListItem(reader("CustomerName"), reader("CustomerId"))
            CustomerNameDropdownList.Items.Add(lstItem)
        End While
        reader.Close()

        connection.Close()

    End Sub

    Protected Sub MembershipTypeDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MembershipTypeDropDownList.SelectedIndexChanged
        Call getPrice()
    End Sub
    Protected Sub getPrice()
        connection.Open()
        str = "select UnitPrice from MembershipCodesTable where Membershipid=@par_MembershipType"
        com = New SqlCommand(str, connection)
        com.Parameters.Add(New SqlParameter("@par_MembershipType", MembershipTypeDropDownList.SelectedValue))
        Dim reader As SqlDataReader = com.ExecuteReader
        If reader.Read Then
            PriceTextBox.Text = reader("UnitPrice").ToString
        End If
        reader.Close()
        connection.Close()
    End Sub
    Private Sub btnGetCustomer_Click(sender As Object, e As EventArgs) Handles btnGetCustomer.Click
        connection.Open()
        'get the customer data from the 
        str = "select Street,City,State,ZipCode,CustomerID from CustomerTable where CustomerID=@par_CustomerID"
        com = New SqlCommand(str, connection)
        com.Parameters.Add(New SqlParameter("@par_CustomerID", CustomerNameDropdownList.SelectedValue))
        Dim reader As SqlDataReader = com.ExecuteReader
        If reader.Read Then
            txtAddress.Text = reader("Street").ToString
            txtCItyStZip.Text = reader("City").ToString + ", " + reader("State").ToString + "  " + reader("ZipCode").ToString
            Session("CustomerID") = reader("CustomerID")
        End If
        reader.Close()
        connection.Close()

        'setting membershiptype back to default
        MembershipTypeDropDownList.SelectedIndex = -1
        PriceTextBox.Text = ""

        'load cart
        loadCart()
    End Sub
    Protected Sub LessonSignupButton_Click(sender As Object, e As EventArgs) Handles LessonSignupButton.Click
        Server.Transfer("Lesson.aspx")
    End Sub

    Protected Sub LockerSignUpButton_Click(sender As Object, e As EventArgs) Handles LockerSignUpButton.Click
        Server.Transfer("SeasonLocker.aspx")
    End Sub

    Protected Sub MainMenuButton_Click(sender As Object, e As EventArgs) Handles MainMenuButton.Click
        Server.Transfer("MainMenu.aspx")
    End Sub

    Protected Sub MaintenanceButton_Click(sender As Object, e As EventArgs) Handles MaintenanceButton.Click
        Server.Transfer("Maintenance.aspx")
    End Sub

    Protected Sub ReportsButton_Click(sender As Object, e As EventArgs) Handles ReportsButton.Click
        Server.Transfer("Reports.aspx")
    End Sub

    Protected Sub RegistrationButton_Click(sender As Object, e As EventArgs) Handles RegistrationButton.Click
        Server.Transfer("Registration.aspx")
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        'Server.Transfer("MembershipListing.aspx")
        MultiView1.SetActiveView(vw_Descriptions)
        rdo_descriptions.SelectedIndex = -1
    End Sub

    Protected Sub CommitButton_Click(sender As Object, e As EventArgs) Handles CommitButton.Click
        Dim cust As String = Session("CustomerID")
        Dim membership As String = MembershipTypeDropDownList.SelectedItem.Text
        Dim price As String = PriceTextBox.Text
        If MembershipTypeDropDownList.SelectedIndex <> 0 And MembershipTypeDropDownList.SelectedIndex <> -1 Then
            Me.CartListBox.Items.Add(membership & " $" & price)

            'inserting into temp cart
            str = "insert into CustomerCart (CustomerID,ItemType,Price) values (" & cust & ",'" & membership & "'," & price & ") "
            Using conn As New SqlConnection(connectionString)
                Using cmd As New SqlCommand(str, conn)
                    conn.Open()
                    cmd.ExecuteNonQuery()

                End Using
            End Using
        End If

    End Sub

    Protected Sub ClearCartButton_Click(sender As Object, e As EventArgs) Handles ClearCartButton.Click
        Dim cust As String = Session("CustomerID")
        Dim membership As String = MembershipTypeDropDownList.SelectedItem.Text
        Dim price As String = PriceTextBox.Text


        'delete from the temp cart
        str = "delete from CustomerCart where CustomerID=" & cust
        ' Update tableName set FieldName1=" & value1 & ",FeildName2='" & value2 & "' where  CustomerID=" & cust
        Using conn As New SqlConnection(connectionString)
            Using cmd As New SqlCommand(str, conn)
                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()
            End Using
        End Using
        Me.CartListBox.Items.Clear()

    End Sub

    Private Sub btnReturn_Click(sender As Object, e As EventArgs) Handles btnReturn.Click
        If rdo_descriptions.SelectedIndex <> -1 Then
            MembershipTypeDropDownList.SelectedValue = rdo_descriptions.SelectedValue
            Call getPrice()
        End If
        MultiView1.SetActiveView(vwSeasonMembership)
    End Sub

    Protected Sub CartButton_Click(sender As Object, e As EventArgs) Handles CartButton.Click
        Server.Transfer("Cart.aspx")
    End Sub
End Class
   
